import datetime

# 六个神煞版本的小六壬（补充五行、方位、提示信息）
SIX_STATES = [
    {
        "name": "大安",
        "element": "木",
        "direction": "正东",
        "meaning": "身不动时，五行属木，颜色青色，方位东方。临青龙，谋事主一、五、七。有静止、心安、吉祥之含义。",
        "hint": "若求急变则不利；若被五行克制，可拜三清（道士常用应对方法）"
    },
    {
        "name": "留连",
        "element": "水",
        "direction": "北方",
        "meaning": "卒未归时，五行属水，颜色黑色，方位北方。临玄武，凡谋事主二、八、十。有喑味不明，延迟、纠缠、拖延、漫长之含义。",
        "hint": "若三个结果中含2个“留连”，建议放弃，避免内耗"
    },
    {
        "name": "速喜",
        "element": "火",
        "direction": "正南",
        "meaning": "人即至时，五行属火，颜色红色，方位南方。临朱雀，谋事主三、六、九。有快速、喜庆、吉利之含义。指时机已到。",
        "hint": "若需稳定，可能转为惊吓；对应“九天应元雷声普化天尊”"
    },
    {
        "name": "赤口",
        "element": "金",
        "direction": "正西",
        "meaning": "官事凶时，五行属金，颜色白色，方位西方。临白虎，谋事主四、七、十。有不吉、惊恐、凶险、口舌是非之含义。",
        "hint": "若含2个“赤口”，易遇身体伤害；对应雷部诸将，凶险度高"
    },
    {
        "name": "小吉",
        "element": "木",
        "direction": "正东",
        "meaning": "人来喜时，五行属木，颜色青色，方位东方。临六合，谋事主一、五、七。有吉祥、顺利、小喜之含义。",
        "hint": "仅能“小利”，无法获得超额收益"
    },
    {
        "name": "空亡",
        "element": "土",
        "direction": "正中",
        "meaning": "音信稀时，五行属土，颜色黄色，方位中央。临勾陈，谋事主三、六、九。有不吉、无结果、忧虑之含义。",
        "hint": "现实事务遇“空亡”需规避，易有实际损失"
    }
]

# 九个神煞版本的小六壬（补充五行、方位、提示信息）
NINE_STATES = [
    {
        "name": "大安",
        "element": "震",
        "meaning": "主平安、顺利，谋事可成，贵人相助。",
        "hint": "若求急变则不利；若被五行克制，可拜三清（道士常用应对方法）"
    },
    {
        "name": "留连",
        "element": "巽",
        "meaning": "主拖延、纠缠，事情进展缓慢，多有阻碍。",
        "hint": "若三个结果中含2个“留连”，建议放弃，避免内耗"
    },
    {
        "name": "速喜",
        "element": "离",
        "meaning": "主喜庆、迅速，事情可快速解决，有喜事降临。",
        "hint": "若需稳定，可能转为惊吓；对应“九天应元雷声普化天尊”"
    },
    {
        "name": "赤口",
        "element": "兑",
        "meaning": "主口舌、是非，易有争执冲突，需防小人。",
        "hint": "若含2个“赤口”，易遇身体伤害；对应雷部诸将，凶险度高"
    },
    {
        "name": "小吉",
        "element": "坎",
        "meaning": "主小吉、顺利，事情有好的结果，可小获成功。",
        "hint": "仅能“小利”，无法获得超额收益"
    },
    {
        "name": "空亡",
        "element": "中",
        "meaning": "主虚空、无果，事情多成空，计划难实现。",
        "hint": "现实事务遇“空亡”需规避，易有实际损失"
    },
    {
        "name": "病符",
        "element": "坤",
        "meaning": "主疾病、灾厄，需注意健康，防意外病痛。",
        "hint": "事件易出问题，需经历“解决问题”的痛苦过程"
    },
    {
        "name": "桃花",
        "element": "艮",
        "meaning": "主感情、人缘，易有感情机遇，人际关系良好。",
        "hint": "除“求姻缘”外，其他事务遇“桃花”易被人牵绊，多为不利"
    },
    {
        "name": "天德",
        "element": "乾",
        "meaning": "主贵人、福德，有天助之运，逢凶化吉，贵人扶持。",
        "hint": "若被“天德”五行所生，必有贵人强力相助，为九位中最利“借力”的位置"
    }
]



def get_six_liuren_result(month, day, hour):
    """六个神煞版本的小六壬计算"""
    # 月上起日
    month_index = (month - 1) % 6
    # 日上起时
    day_index = (month_index + day - 1) % 6
    # 时上起卦
    hour_index = (day_index + hour - 1) % 6
    
    return {
        "month_state": SIX_STATES[month_index],
        "day_state": SIX_STATES[day_index],
        "hour_state": SIX_STATES[hour_index]
    }

def get_nine_liuren_result(month, day, hour):
    """九个神煞版本的小六壬计算"""
    # 月上起日
    month_index = (month - 1) % 9
    # 日上起时
    day_index = (month_index + day - 1) % 9
    # 时上起卦
    hour_index = (day_index + hour - 1) % 9
    
    return {
        "month_state": NINE_STATES[month_index],
        "day_state": NINE_STATES[day_index],
        "hour_state": NINE_STATES[hour_index]
    }

def print_six_result(result):
    """输出六个神煞版本的结果（补充五行、方位和关系分析）"""
    print("====== 六神版小六壬排盘结果 ======")
    date_info = result["original_date"]
    print(f"农历日期: {date_info['lunar_month']}月{date_info['lunar_day']}日 {date_info['hour_name']}")
    
    # 提取三个状态便于分析
    states = [
        result["month_state"],
        result["day_state"],
        result["hour_state"]
    ]
    
    # 输出状态详情（含五行、方位）
    for i, state in enumerate(states):
        stage = ["月卦", "日卦", "时卦"][i]
        print(f"\n【{stage}】: {state['name']}")
        print(f"含义: {state['meaning']}")
        print(f" {state['element']} ")
        print(f"提示: {state['hint']}")
    

def print_nine_result(result):
    """输出九个神煞版本的结果（补充五行、方位和关系分析）"""
    print("\n====== 九神版小六壬排盘结果 ======")
    date_info = result["original_date"]
    print(f"农历日期: {date_info['lunar_month']}月{date_info['lunar_day']}日 {date_info['hour_name']}")
    
    # 提取三个状态便于分析
    states = [
        result["month_state"],
        result["day_state"],
        result["hour_state"]
    ]
    
    # 输出状态详情（含五行、方位）
    for i, state in enumerate(states):
        stage = ["月卦", "日卦", "时卦"][i]
        print(f"\n【{stage}】: {state['name']}")
        print(f"含义: {state['meaning']}")
        print(f" {state['element']}")
        print(f"提示: {state['hint']}")
    


def main():
    """交互界面（保留原逻辑）"""
    print("欢迎使用小六壬排盘工具")
    print("1. 六神版小六壬（大安、留连、速喜、赤口、小吉、空亡）")
    print("2. 九神版小六壬（含天德、桃花、病符等）")
    
    try:
        version_choice = int(input("请选择版本(1/2): "))
        if version_choice not in [1, 2]:
            raise ValueError("请选择1或2")
            
        print("\n农历月份范围：1-12")
        lunar_month = int(input("请输入农历月份: "))
        if not (1 <= lunar_month <= 12):
            raise ValueError("农历月份必须在1-12之间")
            
        print("\n农历日期范围：1-30（根据实际月份调整）")
        lunar_day = int(input("请输入农历日期: "))
        if not (1 <= lunar_day <= 30):
            raise ValueError("农历日期必须在1-30之间")
            
        print("\n农历时辰对应表：")
        print("1: 子时(23-1点)  2: 丑时(1-3点)   3: 寅时(3-5点)")
        print("4: 卯时(5-7点)   5: 辰时(7-9点)   6: 巳时(9-11点)")
        print("7: 午时(11-13点) 8: 未时(13-15点) 9: 申时(15-17点)")
        print("10: 酉时(17-19点) 11: 戌时(19-21点) 12: 亥时(21-23点)")
        lunar_hour = int(input("请输入农历时辰(1-12): "))
        if not (1 <= lunar_hour <= 12):
            raise ValueError("农历时辰必须在1-12之间")
        
        # 准备原始日期信息
        original_date = {
            "lunar_month": lunar_month,
            "lunar_day": lunar_day,
            "lunar_hour": lunar_hour,
            "hour_name": ["", "子时", "丑时", "寅时", "卯时", "辰时", "巳时", 
                          "午时", "未时", "申时", "酉时", "戌时", "亥时"][lunar_hour]
        }
        
        # 根据选择的版本计算并输出结果
        if version_choice == 1:
            result = get_six_liuren_result(lunar_month, lunar_day, lunar_hour)
            result["original_date"] = original_date
            print_six_result(result)
        else:
            result = get_nine_liuren_result(lunar_month, lunar_day, lunar_hour)
            result["original_date"] = original_date
            print_nine_result(result)
            
    except ValueError as e:
        print(f"输入错误: {e}")

if __name__ == "__main__":
    main()
